package com.zhiwang.planet.mvvm.viewmodel.my

import android.annotation.SuppressLint
import android.content.Context
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.zhiwang.planet.BR
import com.zhiwang.planet.R
import com.zhiwang.planet.mvvm.model.RestApi
import com.zhiwang.planet.ui.WebViewActivity
import com.zhiwang.planet.util.androidSchedule
import com.zhiwang.planet.util.responseFunc
import me.tatarka.bindingcollectionadapter2.ItemBinding
import javax.inject.Inject


class CommonPlayedItemViewModel constructor(var icon: String, var name: String, var url: String) {


    lateinit var context: Context


    fun play() {

        WebViewActivity.load(context, url)
    }
}

class MobileViewModel @Inject constructor(var restApi: RestApi, var activity: AppCompatActivity) : ViewModel() {


    val binding = ItemBinding.of<CommonPlayedItemViewModel>(BR.mobileItemViewModel, R.layout.item_mobile_game)

    val items = MutableLiveData<List<CommonPlayedItemViewModel>>()


    val visibility = MutableLiveData<Int>()

    var isMobile = true

    @SuppressLint("CheckResult")
    fun loadMobile() {

        if (isMobile)
            restApi.getMobileList()
                    .androidSchedule()
                    .responseFunc()
                    .subscribe { list ->

                        if (list.isEmpty())
                            visibility.value = View.VISIBLE
                        else visibility.value = View.GONE

                        items.value = list.map { it.toViewModel().apply { context = activity } }
                    }
        else loadFastTask()
    }


    @SuppressLint("CheckResult")
    fun loadFastTask(){
        restApi.getFastTask()
                .androidSchedule()
                .responseFunc()
                .subscribe { list ->

                    if (list.isEmpty())
                        visibility.value = View.VISIBLE
                    else visibility.value = View.GONE

                    items.value = list.map { it.toViewModel().apply { context = activity } }
                }
    }


}